/*
------------------------------------------------------------------------------
readable.c: My random number generator, ISAAC.
(c) Bob Jenkins, March 1996, Public Domain
You may use this code in any way you wish, and it is free.  No warrantee.
* May 2008 -- made it not depend on standard.h
------------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>

#define OUTPUT_BITS (0x7fffffffU)
#define MULTIPLIER (1103515245U)
#define INCREMENT (12345U)

static unsigned long srand_seed = 1;
static unsigned char initialized = 0;

unsigned int xTaskGetTickCount( void );

void srand(unsigned int s)
{
	srand_seed = s;
}

int rand(void)
{
	srand_seed = (MULTIPLIER * srand_seed + INCREMENT) & OUTPUT_BITS;

	return srand_seed;
}

/* attempt to mimic openssl's RAND_bytes and create a cryptographic random
 * number generator.
 */
void RAND_bytes(unsigned char *buf, int num) {
  int i;
  
  if (!initialized) {
    initialized = 1;
	srand(xTaskGetTickCount());
  }
  
  for (i=0; i<num; ++i) {
    buf[i] = (unsigned char) rand();
  }
}

